﻿#ifndef INJECTORTABLEMODEL_H
#define INJECTORTABLEMODEL_H

#include <QtWidgets>
#include <QWidget>
#include "../../include/injectordata.h"
#include "../../include/serialpack.h"

class InjectorTableModel : public QAbstractTableModel
{
public:
    InjectorTableModel();
    ~InjectorTableModel();

    void setHorizontalHeader(const QStringList &headers);
    void setList(const QList<InjectorData>&);

    bool addDevice(const InjectorData &data);
    bool deleteDevice(const QModelIndex &index);

    const InjectorData& getDeviceData(const QModelIndex &index) const;
    InjectorData getDeviceData(const QModelIndex& index) {return  m_list[index.row()];}
    uint getDeviceId(const QModelIndex& index) const;
    InjectorData& lastDevice() {return  m_list.back();}

    void setDeviceData(const QModelIndex& index, const InjectorData& data);
    void updateStatus(const SerialPack& pack);
    //update Settings

    //helper
    const InjectorData& ip2data(int ip) const
    {
        auto it = m_list.begin();
        for(;it!=m_list.end();it++)
            if(it->ip == ip)
                return *it;
        return *m_list.end();
    }

protected:
    //read-onlys
    virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
    virtual QVariant data(const QModelIndex &index, int role) const override;
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    //editable items
    virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;

    //resizable models
    virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
    virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;


private:
    QStringList m_horizontalHeader;
    QList<InjectorData> m_list;
};

#endif // INJECTORTABLEMODEL_H
